﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Obra.Api;
using Obra.SmugMug.Api;
using NUnit.Framework;
using NMock2;

namespace Obra.Test.SmugMug
{
    [TestFixture]
    public class ImageTester :
        NMockTester
    {
        private ISmugMugClient smugMugClient;
        private ISession session;

        private int imageId = 100;
        private int sessionId = 2;

        public override void SetUp()
        {
            base.SetUp();
            this.smugMugClient = this.Mockery.NewMock<ISmugMugClient>();
            this.session = this.Mockery.NewMock<ISession>();
        }

        private void ExpectOnce(
            string method,
            object value)
        {
            Expect.Once.On(this.smugMugClient)
                .Method(method)
                .With(this.sessionId.ToString(), this.imageId, value);
        }

        [Test]
        public void Test()
        {
            Expect.AtLeast(1).On(this.session)
                .GetProperty("SessionId")
                .Will(Return.Value(this.sessionId.ToString()));

            DateTime now = DateTime.Now;

            ImageContract imageContract = new ImageContract()
            {
                //Album = albumContract,
                AlbumUrl = "http://localhost/album",
                Altitude = "altitude",
                Caption = "caption",
                DateString = "1999-01-01 00:00:00",
                FileName = "filename",
                Format = "JPG",
                Height = 1024,
                Hidden = true,
                Id = this.imageId,
                Keywords = "keywords",
                LargeUrl = "http://localhost/album/large.jpg",
                LastUpdatedString = "2000-01-01 00:00:00",
                Latitude = "latitude",
                Longitude = "longitude",
                MD5Sum = "md5sum",
                MediumUrl = "http://localhost/album/medium.jpg",
                OriginalUrl = "http://localhost/album/original.jpg",
                Position = 1,
                Serial = 1,
                Size = 1,
                SmallUrl = "http://localhost/album/small.jpg",
                ThumbUrl = "http://localhost/album/thumb.jpg",
                TinyUrl = "http://localhost/album/tiny.jpg",
                Video1280Url = "http://localhost/album/video1280.avi",
                Video320Url = "http://localhost/album/video320.avi",
                Video640Url = "http://localhost/album/video640.avi",
                Video960Url = "http://localhost/album/video960.avi",
                Watermark = "watermark",
                Width = 1024,
                X2LargeUrl = "http://localhost/album/x2large.jpg",
                X3LargeUrl = "http://localhost/album/x3large.jpg",
                XLargeUrl = "http://localhost/album/xlarge.jpg",
            };

            Image image = new Image(
                imageContract,
                this.session,
                this.smugMugClient);

            Assert.AreEqual("http://localhost/album", image.AlbumUrl.ToString());
            Assert.AreEqual("altitude", image.Altitude);
            Assert.AreEqual("caption", image.Caption);
            Assert.AreEqual(1999, image.Date.Year);
            Assert.AreEqual("filename", image.Name);
            Assert.AreEqual("JPG", image.Format);
            Assert.AreEqual(1024, image.Height);
            Assert.IsTrue(image.Hidden);
            Assert.AreEqual("keywords", image.Keywords);
            Assert.AreEqual("http://localhost/album/large.jpg", image.LargeUrl.ToString());
            Assert.AreEqual(2000, image.LastUpdated.Year);
            Assert.AreEqual("latitude", image.Latitude);
            Assert.AreEqual("longitude", image.Longitude);
            Assert.AreEqual("md5sum", image.MD5Sum);
            Assert.AreEqual("http://localhost/album/medium.jpg", image.MediumUrl.ToString());
            Assert.AreEqual("http://localhost/album/original.jpg", image.OriginalUrl.ToString());
            Assert.AreEqual(1, image.Position);
            Assert.AreEqual(1, image.Serial);
            Assert.AreEqual(1, image.Size);
            Assert.AreEqual("http://localhost/album/small.jpg", image.SmallUrl.ToString());
            Assert.AreEqual("http://localhost/album/thumb.jpg", image.ThumbUrl.ToString());
            Assert.AreEqual("http://localhost/album/tiny.jpg", image.TinyUrl.ToString());
            Assert.AreEqual("http://localhost/album/video1280.avi", image.Video1280Url.ToString());
            Assert.AreEqual("http://localhost/album/video320.avi", image.Video320Url.ToString());
            Assert.AreEqual("http://localhost/album/video640.avi", image.Video640Url.ToString());
            Assert.AreEqual("http://localhost/album/video960.avi", image.Video960Url.ToString());
            Assert.AreEqual("watermark", image.Watermark);
            Assert.AreEqual(1024, image.Width);
            Assert.AreEqual("http://localhost/album/x2large.jpg", image.X2LargeUrl.ToString());
            Assert.AreEqual("http://localhost/album/x3large.jpg", image.X3LargeUrl.ToString());
            Assert.AreEqual("http://localhost/album/xlarge.jpg", image.XLargeUrl.ToString());

            using (this.Mockery.Ordered)
            {
                this.ExpectOnce("ChangeImageCaption", "new");
                this.ExpectOnce("ChangeImageKeywords", "new");
                this.ExpectOnce("ChangeImageHidden", false);
                this.ExpectOnce("ChangeImagePosition", 2);
                this.ExpectOnce("ChangeImageAlbum", 2);
            }

            image.Caption = "new";
            image.Keywords = "new";
            image.Hidden = false;
            image.Position = 2;
            image.AlbumId = 2;
        }
    }
}
